package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.laoxinwen.app.R;

import bean.MainNewsTab;

/**
 * Created by sshong on 15/6/16.
 */
public class MainTopTab extends FrameLayout {
    private MainNewsTab data;
    private int hightLightColor;

    public MainTopTab(Context context) {
        super(context);
    }

    public MainTopTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public MainTopTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }


    public MainTopTab(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.MainTopTab, defStyle, 0);
        hightLightColor = a.getColor(R.styleable.MainTopTab_high_light_color, R.color.theme_blue);
        a.recycle();
    }

    public MainNewsTab getData() {
        return data;
    }

    public void setData(MainNewsTab tab) {
        this.data = tab;
        TextView tv = (TextView) getChildAt(0);
        tv.setText(tab.getTitle());
        requestLayout();
        invalidate();
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        View indicator = (View) getChildAt(1);
        indicator.setVisibility(selected ? VISIBLE : GONE);
        indicator.setBackgroundColor(hightLightColor);
    }
}
